<template>
  <div>
    <el-upload
      class="avatar-uploader"
      action="#"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
      :http-request="upload"
    >
      <img
        v-imgError="defaultImg"
        v-if="imageUrl"
        :src="imageUrl"
        class="avatar"
      />
      <i v-else class="el-icon-plus avatar-uploader-icon" />
    </el-upload>
    <el-progress
      v-if="flag"
      :text-inside="true"
      :stroke-width="24"
      :percentage="percentage"
      status="success"
    ></el-progress>

    <el-progress
      type="dashboard"
      :percentage="percentage"
      :color="colors"
    ></el-progress>
  </div>
</template>

<script>
import defaultImg from '@/assets/404_images/404.png'
export default {
  name: 'UploadImg',
  props: ['imageUrl'],
  data() {
    return {
      // imageUrl: '',
      percentage: 0,
      flag: false,
      colors: [
        { color: '#f56c6c', percentage: 20 },
        { color: '#e6a23c', percentage: 40 },
        { color: '#5cb87a', percentage: 60 },
        { color: '#1989fa', percentage: 80 },
        { color: '#6f7ad3', percentage: 100 }
      ],
      defaultImg: defaultImg
    }
  },
  methods: {
    upload(file) {
      this.flag = true
      /* 1 引入cos  SDK包 */

      var COS = require('cos-js-sdk-v5')
      /* 2-配置秘钥 */
      // SECRETID 和 SECRETKEY请登录 https://console.cloud.tencent.com/cam/capi 进行查看和管理
      var cos = new COS({
        SecretId: 'AKID3vYuznKbaIjtmNltkKegSxDBsIRJuEH3',
        SecretKey: 'XnlTu4wXrtiFWWFhsmOjnb7MK2SbT4OR'
      })
      /* 3-上传对象 */
      cos.putObject(
        {
          Bucket: 'web68-1302684573' /* 必须 */,
          Region: 'ap-nanjing' /* 存储桶所在地域，必须字段 */,
          Key: file.file.name /* 必须 对象键  文件名 */,
          StorageClass: 'STANDARD' /* // 上传模式, 标准模式   */,
          Body: file.file, // 上传文件对象
          onProgress: (progressData) => {
            /* 进度 */
            console.log(JSON.stringify(progressData), 777)
            this.percentage = progressData.percent * 100
          }
        },
        (err, data) => {
          //  成功还是失败都会走这里
          console.log(err || data)
          console.log(data.Location, 666)
          this.flag = false
          this.$emit('update:imgUrl', 'https://' + data.Location)
          this.$emit('success', 'https://' + data.Location)
        }
      )
      console.log(file, 8)
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload(file) {
      const isPNG = file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isPNG) {
        this.$message.error('上传头像图片只能是 PNG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isPNG && isLt2M
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

